package cn.skyclass.business
{
	import cn.skyclass.events.PagerEvent;
	
	import flash.events.EventDispatcher;
	
	/**
	 * 记录和控制分页器的状态
	 * @author fl
	 */	
	[Event(name="pageChanged", type="cn.skyclass.events.PagerEvent")]
	[Event(name="totalPageCountChanged", type="cn.skyclass.events.PagerEvent")]
	[Bindable]
	public class PagerStatus extends EventDispatcher
	{
		public function PagerStatus(){
		}
		
		public static const DEFAULT_PAGE_SIZE:int = 50;
		
		public var pageSize:int = DEFAULT_PAGE_SIZE;
		public var currentPageIndex:int = 1;
		public var totalPageCount:int = -1;
		public var totalRecordCount:int = -1;
		public var recordStartIndex:int = 0;
		
		public var lastPageIndex:int = -1;
		public var isFirstPage:Boolean = true;
		public var isLastPage:Boolean;
		
		/**
		 * 是否有明确的页码总数
		 */		
		public var hasTotalPageCount:Boolean;
		
				
		/**
		 * 更新分页状态
		 * @param totalRecordCount 总记录条数，若 <= 0 则视为未知数目
		 * @param pageSize 分页大小，即每页记录数
		 * @param reset 若为 true ，则将状态重置为第一页；否则维持当前页码并自动验证页码的合法性
		 */						
		public function updateStatus(totalRecordCount:int=-1, 
			pageSize:int=50, reset:Boolean=false):void{
			
			this.totalRecordCount = totalRecordCount;	
			this.pageSize = pageSize;
			
			// get new totalPageCount
			var newTotalPageCount:int = this.totalPageCount;
			if(totalRecordCount >= 0){
				newTotalPageCount = totalRecordCount / pageSize;
				if((newTotalPageCount == 0) || (totalRecordCount % pageSize) > 0){
					newTotalPageCount++;
				}
			}else{
				newTotalPageCount = -1;
			}
			
			// update totalPageCount
			if(newTotalPageCount != this.totalPageCount){
				this.totalPageCount = newTotalPageCount;
				
				// set hasTotalPageCount
				this.hasTotalPageCount = (this.totalPageCount > 0);
				
				dispatchTotalPageCountChangedEvent();
			}
			
			// update currentPageIndex
			var newCurrentPageIndex:int = this.currentPageIndex;
			if(reset){
				newCurrentPageIndex = 1;
			}else if(hasTotalPageCount && 
				(this.currentPageIndex > this.totalPageCount)){
				newCurrentPageIndex = 1;
			}
			if(newCurrentPageIndex != this.currentPageIndex){
				this.currentPageIndex = newCurrentPageIndex;
				dispatchPageChangedEvent();
			}
			
			// regenerate some state values
			this.isFirstPage = (this.currentPageIndex == 1);
			this.recordStartIndex = this.pageSize * (this.currentPageIndex - 1);
			this.lastPageIndex = this.hasTotalPageCount ? this.totalPageCount : -1;
			this.isLastPage = this.hasTotalPageCount ? (this.currentPageIndex == this.lastPageIndex) : false;
		}		
		
		/**
		 * 当未知总页码数时，可以使用此方法检测每一页是否是最后一页。
		 * 即当页内的记录数不足分页大小时则视为最后一页。若是最后一页则自动更新当前状态。
		 * @param pageRecordCount
		 * @param pageIndex
		 * @return 
		 */		
		public function checkForLastPage(pageRecordCount:int, pageIndex:int):Boolean{
			if((pageRecordCount < pageSize) &&
				(pageRecordCount >= 0)){
				var totalRecordCount:int = pageSize * (pageIndex - 1) 
					+ pageRecordCount;
				updateStatus(totalRecordCount, pageSize);
				return true;
			}else{
				return false;
			}
		}
		
		
		public function toFirstPage():Boolean{
			return changePage(1);
		}

		public function toPrevPage():Boolean{
			return changePage(this.currentPageIndex - 1);
		}

		public function toNextPage():Boolean{
			return changePage(this.currentPageIndex + 1);
		}

		public function toLastPage():Boolean{
			return this.hasTotalPageCount ? changePage(this.lastPageIndex) : false;
		}

		public function toPage(newPageIndex:int):Boolean{
			return this.hasTotalPageCount ? changePage(newPageIndex) : false;
		}
		
		protected function changePage(newPageIndex:int):Boolean{
			// validate new index
			if((newPageIndex == currentPageIndex) || (newPageIndex < 1) 
				|| greaterThanMaxPageIndex()){
				return false;
			}
			
			this.currentPageIndex = newPageIndex;
			
			// regenerate some state values
			this.isFirstPage = (this.currentPageIndex == 1);
			this.recordStartIndex = this.pageSize * (this.currentPageIndex - 1);
			this.isLastPage = this.hasTotalPageCount ? (this.currentPageIndex == this.lastPageIndex) : false;			
			
			dispatchPageChangedEvent();	
			return true;
			
			function greaterThanMaxPageIndex():Boolean{
				return hasTotalPageCount && (newPageIndex > totalPageCount);
			}
		}
		
		protected function dispatchPageChangedEvent():void{
			var e:PagerEvent = new PagerEvent(PagerEvent.PAGE_CHANGED, this.currentPageIndex);
			e.pageSize = this.pageSize;
			e.totalPageCount = this.totalPageCount;
			dispatchEvent(e);
		}
		
		protected function dispatchTotalPageCountChangedEvent():void{
			var e:PagerEvent = new PagerEvent(
				PagerEvent.TOTAL_PAGE_COUNT_CHANGED, this.currentPageIndex);
			e.pageSize = this.pageSize;
			e.totalPageCount = this.totalPageCount;
			dispatchEvent(e);
		}
					
	}
}